/*
 * patterns.h
 *
 *  Created on: 26/mag/2014
 *      Author: PegorarS
 */

#ifndef PATTERNS_H_
#define PATTERNS_H_

#include <vector>
using namespace std;

template<class T> class Subscriber{
public:
	virtual ~Subscriber() = 0;

	virtual void Update(T &dataPublished) = 0;
};


template<class T> class Publisher{
public:
	virtual ~Publisher() = 0;

	void Attach(Subscriber<T> *subToAdd){
		subs.push_back(subToAdd);
	}

	virtual void Publish(T &dataToPublish) = 0;

	void Detach(Subscriber<T> *subToRemove){
		for (int i= 0; i< subs.size(); i++){
			if (subs[i]== subToRemove){
				subs.erase(subs.begin()+i);
				return;
			}
		}
	}

private:
	vector<class Subscriber<T> *> subs;
};

#endif /* PATTERNS_H_ */
